﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AMAP.Areas.Client.Models.Account;
using AMAP.Domain;
using AMAP.ServiceInterfaces;
using System.Web.Security;

namespace AMAP.Areas.Client.Controllers
{
    public class AccountController : Controller
    {
        private IClientService ClientService;
        private IPlaceService PlaceService;

        public AccountController(IClientService ClientService, IPlaceService PlaceService)
        {
            this.ClientService = ClientService;
            this.PlaceService = PlaceService;
        }

        //
        // GET: /Client/Account/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            ClientAccountCreateViewModel Model = new ClientAccountCreateViewModel();
            Model.PlaceList = new SelectList(PlaceService.GetAllAsList(), "Id", "Address1");
            return View(Model);
        }

        [HttpPost]
        public ActionResult Create(ClientAccountCreateViewModel Model)
        {
            if (ModelState.IsValid)
            {
                Domain.Client Client = ClientService.Create();

                AutoMapper.Mapper.Map(Model, Client);
                AutoMapper.Mapper.Map(Model, Client.User);

                ClientService.Add(Client);
                //Connect the registrated user
                FormsAuthentication.SetAuthCookie(Model.Email, false);

                return RedirectToRoute("Client_default", new { Controller = "Home", action = "Index" });
            }

            return View(Model);
        }

        [Authorize(Roles = "Client")]
        public ActionResult Edit()
        {
            throw new NotImplementedException();
        }
    }
}
